<?php namespace Common\Model;
use Hdphp\Model\Model;
//用户管理模型
class Good extends Model{
	//指定表名
    protected $table = "Good";
    protected $validate = array(
       array('gname','required','商品名称不能为空',3,3),
       // array('bsort','num:0,65535','排序范围0-65535',3,3),
    );
    protected $auto = array(
        array('gtime','time','function',3,3),
      );


   // 自定义store方法
    public function store(){ 
    	if(!$this->create()) return  false;
    		$gid=$this->add();
        $gdetail = new \Common\Model\Gdetail;
        if(!$gdetail->store()) return  false;
        $gdetail->data['good_gid']=$gid;
        $gdetail->add();
    		$gnatureModel=new \Common\Model\Gnature;
        // $gnatureModel->add();
    	  foreach ($_POST['attr'] as $k => $v) {
            $attr=array(
                  'gnvalue'=>$v,
                  'typeattr_taid'=>$k,
                  'good_gid'=>$gid,
              );
            $gnatureModel->add($attr);
        }
      // p($attr);
    		foreach ($_POST['spec'] as $key => $value) {
          foreach ($value['value'] as $k => $v) {
            $spec=array(
                  'gnvalue'=>$v,
                  'typeattr_taid'=>$key,
                  'good_gid'=>$gid,
                  'gaddprice'=>$value['price'][$k],
              );
            $gnatureModel->add($spec);
          }
        }
    	return true;
    }

    public function edit(){

        if(!$this->create()) return  false;
        $this->save();
        $gdetail = new \Common\Model\Gdetail;
        if(!$gdetail->store()) return  false;
        $gdetail->data['good_gid']=Q('post.gid');
        $gdetail->where('good_gid='.Q('post.gid'))->save();
        $gnatureModel=new \Common\Model\Gnature;
        
        foreach ($_POST['attr'] as $k => $v) {
            $attr=array(
                  'gnvalue'=>$v,
                  'typeattr_taid'=>$k,
                  'good_gid'=>Q('post.gid'),
              );
            $gnatureModel->add($attr);
        }
      // p($attr);
        foreach ($_POST['spec'] as $key => $value) {
          foreach ($value['value'] as $k => $v) {
            $spec=array(
                  'gnvalue'=>$v,
                  'typeattr_taid'=>$key,
                  'good_gid'=>Q('post.gid'),
                  'gaddprice'=>$value['price'][$k],
              );
            $gnatureModel->add($spec);
          }
        }
      return true;
    }
}
?>